﻿using Cysharp.Threading.Tasks;
using Net.Client;
using Net.Event;
using Net.Helper;
using Net.Server;
using Net.Share;
using System.Diagnostics;

namespace NetworkTest
{
    internal class TestRpcA
    {
        public string msg;

        [Rpc(NetCmd.SafeCallHash, 1001)]
        internal void Test(ClientPeer client)
        {
            NDebug.Log(msg);
        }
    }

    internal class TestRpcB
    {
        public string msg;

        [Rpc(1001)]
        internal void Test()
        {
            NDebug.Log(msg);
        }
    }

    internal class ClientPeer : ClientPeerBase
    {
        internal TestRpcA testRpcA = new() { msg = "testRpcA" };
        internal TestRpcA testRpcA1 = new() { msg = "testRpcA11111" };
        internal TestRpcB testRpcB = new() { msg = "testRpcB" };

        public override void OnStart()
        {
            base.OnStart();
            AddRpc(testRpcA);
            AddRpc(testRpcA1);
            AddRpc(testRpcB);
        }

        [Rpc(1001)]
        internal void Test()
        {
            NDebug.Log("Test");
        }
    }

    public class CollectTypes : IAssemblyTypeHashCollect
    {
        public Type[] Types => [typeof(List<TestRpcA>), typeof(List<TestRpcB>)];
    }

    internal class Program
    {
        static Stopwatch stopwatch;

        static void Main(string[] args)
        {
            var x = typeof(CollectTypes);


            NDebug.BindConsoleLog();

            var program = new Program();

            var server = new TcpServer<ClientPeer, SceneBase<ClientPeer>>();
            server.Config.LimitQueueCount = 10000000;//测试小数据的快速性能, 可以设置这里, 默认限制在65536
            server.Config.PackageLength = 10000000;//小数据包封包合包大小, 一次性能运送的小数据包数量
            server.Config.PackageSize = 1024 * 1024 * 50;//接收缓存数据包的最大值, 如果超出则被丢弃
            server.Config.SendBufferSize = 1024 * 1024 * 10; //发送窗口设置
            server.Config.ReceiveBufferSize = 1024 * 1024 * 10; //接收窗口设置
            //server.AddAdapter(new Net.Adapter.SerializeAdapter3());//采用极速序列化进行序列化rpc数据模型
            //server.AddAdapter(new Net.Adapter.CallSiteRpcAdapter());//采用极速调用rpc方法适配器
            server.RpcAdapter.AddRpc(program);
            server.OnReceiveData = (client, model) =>
            {
                var i = model.AsInt;
                if (i % 10000 == 0)
                    Console.WriteLine(i);
                if (i >= 999999)
                {
                    stopwatch.Stop();
                    Console.WriteLine(stopwatch.Elapsed);
                }
            };
            server.Start();

            var client = new TcpClientUnit();
            client.Config.LimitQueueCount = 10000000;
            client.Config.PackageLength = 10000000;
            client.Config.PackageSize = 1024 * 1024 * 50;
            client.Config.SendBufferSize = 1024 * 1024 * 10; //发送窗口设置
            client.Config.ReceiveBufferSize = 1024 * 1024 * 10; //接收窗口设置
            //client.AddAdapter(new Net.Adapter.SerializeAdapter3());
            //client.AddAdapter(new Net.Adapter.CallSiteRpcAdapter());
            client.RpcAdapter.AddRpc(program);
            client.OnReceiveData = (model) =>
            {
                var i = model.AsInt;
                if (i % 10000 == 0)
                    Console.WriteLine(i);
                if (i >= 999999)
                {
                    stopwatch.Stop();
                    Console.WriteLine(stopwatch.Elapsed);
                }
            };
            client.Connect().AsTask().Wait();

            stopwatch = Stopwatch.StartNew();

            client.Call(new byte[1]);

            client.Call(NetCmd.EntityRpc, 1001);
            //client.Call(NetCmd.EntityRpc, "Test");
            object list = new List<TestRpcA> { new TestRpcA() { msg = "aoe" } };
            client.Call(3, list);

            Console.ReadLine();

            for (int i = 0; i < 1000000; i++)
            {
                //client.Call(NetCmd.Local, 2, i);
                client.Call(1, i);
                if (i % 10000 == 0)
                    Thread.Sleep(1);
            }

            Console.ReadLine();
        }

        [Rpc(NetCmd.SafeCallHash, 1)]
        void test1(ClientPeer client, int i)
        {
            client.Call(2, i);
        }

        [Rpc(2)]
        void test2(int i)
        {
            if (i % 10000 == 0)
                Console.WriteLine(i);
            if (i >= 999999)
            {
                stopwatch.Stop();
                Console.WriteLine(stopwatch.Elapsed);
            }
        }

        [Rpc(NetCmd.SafeCallHash, 3)]
        internal void test3(ClientPeer client, List<TestRpcA> list)
        {
        }
    }
}
